package aad.storyboard.model;

	
	

	import aad.storyboard.R;
	import aad.storyboard.controller.AudioPlayer;
	import aad.storyboard.controller.GUIComposer;
	import aad.storyboard.controller.MenuViewProvider;
	import aad.storyboard.controller.NavigationClickListener;
	import aad.storyboard.transitions.SlideTransition;
	import aad.storyboard.view.WikiView;
	import android.app.Activity;
	import android.graphics.BlurMaskFilter;
	import android.graphics.Color;
	import android.graphics.Paint;
	import android.os.Handler;
	import android.view.View;
	import android.view.ViewGroup;
	import android.webkit.WebSettings;
	import android.webkit.WebView;
	import android.widget.ImageButton;
	import android.widget.ViewFlipper;

	public class InfoAnimation extends Animation{
	        
	                
	        static ViewFlipper flipper; 
	        private static Handler mHandler=new Handler();
	        private static Image currentImage;
	         
	        private static WikiView wikiView;       
	        private  String text;
	        private static String file;
	        private static Activity activity;
	        private boolean touched;
	        private static boolean undo;
	        
	        private static String jsdata;
	        
	        public InfoAnimation(int start, int duration, Activity a){
	                super(duration, start);
	                flipper=GUIComposer.getInstance().getFlipper(); 
	                activity=a;     
	                
	        }
	        
	        @Override
	        public void animate() {
	                        
	                                
	                 if (touched)           {
	                         
	                     text=currentImage.getInfo();                       
	                     showExplanation(text);
	                     touched=false;
	                }
	                
	                
	        }

	        @Override
	        public void adjustParameters() {
	                // TODO Auto-generated method stub
	                
	        }
	        
	        public void showExplanation(String word)
	    {   
	        //file="file:///android_asset/"+word+".html"; 
	                jsdata = "<script type=\"text/javascript\">function showWikiPage("+currentImage.getClickableWord()+"){Android.showExplanation("+currentImage.getClickableWord()+");}</script>";
	        mHandler.removeCallbacks(mTransitionTask);              
	            mHandler.postDelayed(mTransitionTask, 100);             
	                        
	    
	    }
	    
	    private static Runnable mTransitionTask = new Runnable() {
	                   @Override
	                public void run() {                        
	                           activity.setContentView(R.layout.wikipage);
	                           wikiView=(WikiView)activity.findViewById(R.id.wikiView);
	                           //wikiView.loadUrl(file);
	                           WebSettings ws = wikiView.getSettings();
	                           ws.setJavaScriptEnabled(true);                         
	                           wikiView.loadData(currentImage.getInfo()+jsdata,  "text/html", "utf-8");
	                           wikiView.setjInterFace(activity);
	                           ImageButton backtomain=(ImageButton)activity.findViewById(R.id.back);
	                           backtomain.setOnClickListener(NavigationClickListener.getInstance());
	                   }
	                };

	        

	        public void setCurrentImage(Image img) {
	                this.currentImage=img;
	                
	        }
	        
	        public void setTouched(boolean t)
	        {
	                touched=t;
	        }
	        
	        @Override
	        public void undo()
	        {
	                View mainView=MenuViewProvider.getInstance().getMainMenuView();
	                if(mainView.getParent() != null){
	                        ((ViewGroup)mainView.getParent()).removeView(mainView);
	                }
	                activity.setContentView(mainView);      
	        }

	
	
}
